using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Update.Controls
{
    /// <summary>
    /// 自定义滚动条类，用于在 TextBox 和 RichTextBox 控件中使用
    /// </summary>
    public static class DarkScrollBar
    {
        // Windows API 常量
        private const int WM_NCCALCSIZE = 0x0083;
        private const int SB_HORZ = 0;
        private const int SB_VERT = 1;
        private const int SB_BOTH = 3;
        private const int ESB_ENABLE_BOTH = 0x0000;
        private const int ESB_DISABLE_BOTH = 0x0003;
        private const int SIF_RANGE = 0x0001;
        private const int SIF_PAGE = 0x0002;
        private const int SIF_POS = 0x0004;
        private const int SIF_DISABLENOSCROLL = 0x0008;
        private const int SIF_TRACKPOS = 0x0010;
        private const int SIF_ALL = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS;

        // 滚动条信息结构
        [StructLayout(LayoutKind.Sequential)]
        private struct SCROLLINFO
        {
            public int cbSize;
            public int fMask;
            public int nMin;
            public int nMax;
            public int nPage;
            public int nPos;
            public int nTrackPos;
        }

        // Windows API 函数
        [DllImport("user32.dll")]
        private static extern int GetScrollInfo(IntPtr hWnd, int fnBar, ref SCROLLINFO lpsi);

        [DllImport("user32.dll")]
        private static extern int SetScrollInfo(IntPtr hWnd, int fnBar, [In] ref SCROLLINFO lpsi, bool fRedraw);

        [DllImport("user32.dll")]
        private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);

        [DllImport("user32.dll")]
        private static extern bool EnableScrollBar(IntPtr hWnd, int wSBflags, int wArrows);

        [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
        private static extern int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList);

        /// <summary>
        /// 应用深色主题滚动条样式到控件
        /// </summary>
        /// <param name="control">要应用样式的控件</param>
        public static void ApplyTheme(Control control)
        {
            if (control == null || !control.IsHandleCreated)
                return;

            try
            {
                // 应用 Windows 10 现代滚动条样式
                SetWindowTheme(control.Handle, "DarkMode_Explorer", null);
            }
            catch (Exception)
            {
                // 忽略异常
            }
        }

        /// <summary>
        /// 设置滚动条的宽度
        /// </summary>
        /// <param name="control">要设置的控件</param>
        /// <param name="width">滚动条宽度</param>
        public static void SetScrollBarWidth(Control control, int width)
        {
            if (control == null || !control.IsHandleCreated)
                return;

            try
            {
                // 设置滚动条信息
                SCROLLINFO si = new SCROLLINFO();
                si.cbSize = Marshal.SizeOf(si);
                si.fMask = SIF_ALL;

                // 获取当前滚动条信息
                GetScrollInfo(control.Handle, SB_VERT, ref si);

                // 修改滚动条宽度 (这个功能在 Windows 10 中可能不起作用，因为滚动条宽度由系统主题控制)
                // 但我们可以尝试通过修改滚动条的页面大小来间接影响其宽度
                si.nPage = Math.Max(1, width);
                SetScrollInfo(control.Handle, SB_VERT, ref si, true);
            }
            catch (Exception)
            {
                // 忽略异常
            }
        }

        /// <summary>
        /// 为 TextBox 或 RichTextBox 控件添加消息处理器以自定义滚动条
        /// </summary>
        /// <param name="textBox">要处理的文本框控件</param>
        public static void AttachToTextBox(TextBoxBase textBox)
        {
            if (textBox == null)
                return;

            // 确保控件使用滚动条
            if (textBox is TextBox)
            {
                ((TextBox)textBox).ScrollBars = ScrollBars.Vertical;
            }
            else if (textBox is RichTextBox)
            {
                ((RichTextBox)textBox).ScrollBars = RichTextBoxScrollBars.Vertical;
            }

            // 应用主题
            if (textBox.IsHandleCreated)
            {
                ApplyTheme(textBox);
            }
            else
            {
                textBox.HandleCreated += (sender, e) => ApplyTheme(textBox);
            }

            // 设置滚动条宽度
            textBox.Resize += (sender, e) => SetScrollBarWidth(textBox, 8);
        }
    }
} 